//
//  DBJHomeViewController.m
//  DBJ
//
//  Created by 花化华 on 2016/11/12.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "DBJHomeViewController.h"
#import "DBJRegisterViewController.h"
#import "DBJPBenefitViewController.h"
#import "DBJWeatherModel.h"
#import "DBJNetworkTool.h"
#import <UIImageView+WebCache.h>

@interface DBJHomeViewController ()

/** 5个挂诊ViewController的title */
@property (nonatomic,strong)NSArray *titles;

@property (weak, nonatomic) IBOutlet UIImageView *weatherImagView;
@property (weak, nonatomic) IBOutlet UILabel *temperatureLabel;
@property (weak, nonatomic) IBOutlet UILabel *weatherLabel;

/** 天气模型 */
@property (strong,nonatomic) DBJWeatherModel *weatherModel;

@end

@implementation DBJHomeViewController

- (void)dbjSetupNavigationItems{
    [super dbjSetupNavigationItems];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
}

- (void)dbjSetupSubviews{
    [super dbjSetupSubviews];
    
    //设置数据
    [self loadWeatherData];
    _titles = @[@"肿瘤",@"血液科",@"心血管",@"神经科",@"骨科"];
}

/**
 * 加载天气数据
 */
- (void)loadWeatherData{
    
    //天气key
    NSString *key = @"17IvzuqmKrK1cGwNL6VQebF9";
    
    //网络获取
    [[DBJNetworkTool sharedNetworkTool]GETWithURLString:@"http://api.map.baidu.com/telematics/v3/weather" parameters:@{@"location":@"深圳",@"output":@"json",@"ak":key} success:^(id responseObject) {
        
        //反序列化
        NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:NULL];
        
        //responseDict转模型
        NSArray *result = responseDict[@"results"];
        NSDictionary *resultDict = result.firstObject;
        NSArray *weatherData = resultDict[@"weather_data"];
        NSDictionary *weatherDataDict = weatherData.firstObject;
        _weatherModel = [DBJWeatherModel weatherWithDict:weatherDataDict];
        
        //更新数据
//        [_weatherImagView sd_setImageWithURL:[NSURL URLWithString:_weatherModel.dayPictureUrl]];
        _temperatureLabel.text = _weatherModel.temperature;
        _weatherLabel.text = [NSString stringWithFormat:@"深圳(%@) %@",_weatherModel.weather,_weatherModel.date];
        
    } faile:^(NSError *error) {
        NSLog(@"网络请求错误：%@",error);
    }];
    
}

/**
 * 5个挂诊的入口
 */
- (IBAction)registerClick:(UIButton *)sender {
    DBJRegisterViewController *registerViewController = [[DBJRegisterViewController alloc]init];
    registerViewController.title = _titles[sender.tag];
    [self.navigationController pushViewController:registerViewController animated:YES];
}


/**
 * 公益活动入口
 */
- (IBAction)pbenefitClick:(UIButton *)sender {
    DBJPBenefitViewController *pbenefitViewController = [[DBJPBenefitViewController alloc]init];
    pbenefitViewController.title = @"公益活动";
    [self.navigationController pushViewController:pbenefitViewController animated:YES];
}

@end
